/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.impl;

import com.ceair.flight.refund.dto.appmoblie.*;
import com.ceair.flight.refund.dto.external.BasePageQueryReq;
import com.ceair.flight.refund.location.dao.IRefundApplyDao;
import com.ceair.flight.refund.location.dt.enums.*;
import com.ceair.flight.refund.service.cronjob.AutoFirstAuditService;
import com.ceair.flight.refund.service.cronjob.autocheck.IAutoAuditCheckerChain;
import com.ceair.flight.refund.util.DBUtil;
import com.ceair.flight.refund.util.RefundUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author youguilin
 */
@Service
public class AutoFirstAuditServiceImpl extends AbstractScheduleServiceImpl implements AutoFirstAuditService {

    private static final Logger LOG = LoggerFactory.getLogger(AutoFirstAuditServiceImpl.class);
    //对所有7天以内的在等待审核的申请订单进行自动审核
    private static final int HOURS_BEFORE = -24 * 7;
    @Autowired
    private IAutoAuditCheckerChain autoAuditCheckerChain;
    @Value("${auto.first.audit.switch}")
    private Boolean autoAuditSwith;

    @Autowired
    private IRefundApplyDao applyDao;

    @Override
    public void doRefundApplyAudit(RefundApplyDto refundApply) {
        boolean isAutoAuditable = isAutoAuditable(refundApply);
        if (isAutoAuditable) {
            LOG.info("Refund apply {} pass auto first audit", refundApply.getId());
            saveAuditableData(refundApply);
        } else {
            LOG.info("Refund apply {} auto first audit failed", refundApply.getId());
            saveUnauditableData(refundApply);
        }
    }

    /**
     * 检查是否可自动审核
     *
     * @param refundApply
     * @return
     */
    private boolean isAutoAuditable(RefundApplyDto refundApply) {
        return autoAuditCheckerChain.isAutoAuditable(refundApply);
    }

    /**
     * 保存自动审核数据
     *
     * @param refundApply
     */
    private void saveAuditableData(RefundApplyDto refundApply) {
        RefundAuditDto refundAudit = buildRefundAudit(AuditStatus.PASS);
        refundApply.addRefundAudit(refundAudit);
        refundApply.setUpdatDate(new Date());
        LOG.info("Pass auto first audit condition, changing refund apply {} stauts to: {}", refundApply.getId(), RefundStatusEnum.WAIT_SECOND_AUDIT);
        refundApply.setStatus(RefundStatusEnum.WAIT_SECOND_AUDIT.getCode());
        refundApply.setAutoFirstAudit(RefundAutoAuditEnum.PASS.getCode());
        //mybatis写法
//        applyDao.updateRefundApply(refundApply);
//        applyDao.insertRefundAudit(refundAudit);
    }

    /**
     * 保存自动审核数据
     *
     * @param refundApply
     */
    private void saveUnauditableData(RefundApplyDto refundApply) {
        RefundAuditDto refundAudit = buildRefundAudit(AuditStatus.FAIL);
        refundApply.addRefundAudit(refundAudit);
        refundApply.setUpdatDate(new Date());
        LOG.info("Unable to auto first audit, keeping refund apply {} stauts in: {}", refundApply.getId(), RefundStatusEnum.WAIT_FIRST_AUDIT);
        refundApply.setAutoFirstAudit(RefundAutoAuditEnum.FAIL.getCode());
        //mybatis写法
//        applyDao.updateRefundApply(refundApply);
//        applyDao.insertRefundAudit(refundAudit);
    }

    private RefundAuditDto buildRefundAudit(AuditStatus status) {
        RefundAuditDto audit = new RefundAuditDto();
        audit.setAuditorName("SYSTEM");
        audit.setAuditDate(new Date());
        audit.setAuditStatus(status.getCode());
        if (status == AuditStatus.PASS) {
            audit.setAuditDesc("系统自动一审通过");
        } else {
            audit.setAuditDesc("自动一审未通过，需人工审核");
        }
        audit.setAuditPhase(String.valueOf(AuditPhase.FIRST_ADUIT.getCode()));
        return audit;
    }

    @Override
    void doScheduleTask(RefundApplyDto refundApply) {
        doRefundApplyAudit(refundApply);
    }

    @Override
    BasePageQueryReq buildQueryReq() {
        BasePageQueryReq pageQueryReq = new BasePageQueryReq();
        pageQueryReq.setPageSize(50);
        pageQueryReq.addRefundStatus(RefundStatus.WAIT_FIRST_AUDIT);
        pageQueryReq.setAutoFirstStatus(RefundAutoAuditEnum.DEFAULT);
        Date sysdate = new Date();
        pageQueryReq.setUpdateBegin(RefundUtil.getDate(sysdate, HOURS_BEFORE));
        pageQueryReq.setUpdateEnd(sysdate);
        return pageQueryReq;
    }

}
